package com.cheney.weiboo.view;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheney.weiboo.R;
import com.cheney.weiboo.domain.Users;
import com.cheney.weiboo.service.UserService;
import com.cheney.weiboo.utils.AsyncImageLoader;
import com.cheney.weiboo.utils.AsyncImageLoader.ImageCallback;
import com.mime.qweibo.examples.MyWeiboSync;

public class UserInfoActivity extends Activity {

	private TextView info_nick;
	private ImageView info_headicon;
	private AsyncImageLoader asyncImageLoader;
	private UserService userService;
	private List<Users> usersList;
	private Users user;
	private MyWeiboSync weibo;
	private TextView info_at;
	private TextView info_origtext;
	private TextView info_time;
	private JSONObject jsonObject;
	private String jsonStr;
	private String name;
	private UserInfoHandler handler;
	private Drawable cachedImage;
	private GridView  gridView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_info);
		
		setViews();
		setListeners();
		asyncImageLoader = new AsyncImageLoader();
		userService = new UserService(this);
		usersList = userService.getUserList(false);
		weibo = new MyWeiboSync();
		SharedPreferences sp = getSharedPreferences("default_users", Context.MODE_PRIVATE);
		String nick = sp.getString("user_default_nick", "");
		
		if(nick!=null){
			user = userService.getUserByName(nick, usersList);
		}
		
		weibo.setAccessTokenKey(user.getToken());
		weibo.setAccessTokenSecrect(user.getTokenSecret());
		
		Intent intent = getIntent();
		name = intent.getStringExtra("name");
		info_at.setText(name);
		info_nick.setText(intent.getStringExtra("nick"));
		info_origtext.setText(intent.getStringExtra("origtext"));
		info_time.setText(intent.getStringExtra("time"));
		
		handler = new UserInfoHandler();
		UserInfoThread userInfoThread = new UserInfoThread();
		userInfoThread.start();
	}
	
	public void setViews(){
		info_headicon = (ImageView)findViewById(R.id.info_headicon);
		info_nick = (TextView)findViewById(R.id.info_nick);
		info_at = (TextView)findViewById(R.id.info_at);
		info_origtext = (TextView)findViewById(R.id.info_origtext);
		info_time = (TextView)findViewById(R.id.info_time);
		gridView = (GridView)findViewById(R.id.info_grid);
	}
	
	public void setListeners(){
		
	}
	
	class UserInfoHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			try {
				jsonObject = new JSONObject(jsonStr).getJSONObject("data");
				cachedImage = asyncImageLoader.loadDrawable(jsonObject.getString("head")+"/100",info_headicon, new ImageCallback(){
	                @Override
	                public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl) {
	                    imageView.setImageDrawable(imageDrawable);
	                }
	            });
				if(cachedImage!=null){
					info_headicon.setImageDrawable(cachedImage);
				}else {
					info_headicon.setImageResource(R.drawable.icon);
				}
				
				List<String> numList = new ArrayList<String>();
				numList.add(jsonObject.getString("tweetnum"));
				numList.add(jsonObject.getString("fansnum"));
				numList.add(jsonObject.getString("idolnum"));
				gridView.setAdapter(new GridAdapter(UserInfoActivity.this,numList));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	class UserInfoThread extends Thread {

		@Override
		public void run() {
			jsonStr = weibo.getUserInfoByName(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), name);
			Message msg = handler.obtainMessage();
			handler.sendMessage(msg);
		}
		
	}
	
	class GridAdapter extends BaseAdapter{

		private Context context;
		private List<String> numList;
		private LayoutInflater inflater;
		
		public GridAdapter(Context context,List<String> numList){
			this.numList = numList;
			this.context = context;
			this.inflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			
			return numList.size();
		}

		@Override
		public Object getItem(int position) {
			
			return numList.get(position);
		}

		@Override
		public long getItemId(int position) {
			
			return position;
		}

		@Override
		public View getView(int position, View contentView, ViewGroup arg2) {
			contentView = inflater.inflate(R.layout.user_info_item, null);
			TextView num = (TextView)contentView.findViewById(R.id.userinfo_grid_num);
			TextView title = (TextView)contentView.findViewById(R.id.userinfo_grid_title);
			ImageView image = (ImageView)contentView.findViewById(R.id.userinfo_grid_image);
			switch(position){
			case 0: 
				num.setText(numList.get(0));
				title.setText("广播");
				image.setVisibility(View.VISIBLE);
			break;
			case 1:
				num.setText(numList.get(1));
				title.setText("听众");
				image.setVisibility(View.VISIBLE);
			break;
			case 2:
				num.setText(numList.get(2));
				title.setText("收听");
			break;
			}
			return contentView;
		}
		
	}
}
